﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WatiN.Core;
using System.Text.RegularExpressions;

namespace WatiN.Sharepoint.Pages.CentralAdmin
{
    public class ListContentSourcesPage : BaseCentralAdminPage
    {
        public ListContentSourcesPage()
            : base()
        {

        }

        public override string RelativePageUrl
        {
            get { return "_admin/search/listcontentsources.aspx"; }
        }
        
        public Table ContentSourcesList
        {
            get
            {
                return Document.Table(new Regex("listContentSourcesControl_gridView$"));
            }
        }

        public TableRow GetContentSourceItem(string contentSource)
        {
            
            return ContentSourcesList.TableRow(Find.ByTextInColumn(contentSource, 1));
        }

        public void StartFullCrawl(string contentSource)
        {
            string crawlType = "Start Full Crawl";

            StartCrawl(contentSource, crawlType);
        }

        public void StartIncrementalCrawl(string contentSource)
        {
            string crawlType = "Start Incremental Crawl";

            StartCrawl(contentSource, crawlType);
        }

        private void StartCrawl(string contentSource, string crawlType)
        {
            TableRow contentSourceRow = GetContentSourceItem(contentSource);
            Image contentsource = contentSourceRow.Image(Find.ByUrl("http://spdev01:3333/_layouts/images/menudark.gif"));

            contentsource.Click();

            Span crawlLink = Document.Span(Find.ByText(crawlType));
            crawlLink.Click();
        }

    }
}
